package com.uenpay.im.invokeitems.friends;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.entity.FriendEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;
import com.uenpay.im.serializations.FriendSerializer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取好友列表
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetFriends extends BaseHttpInvokeItem {
	/**
	 * 
	 * @author SL
	 * 
	 */
	public class getAllFriendResult extends HttpInvokeResult {
		public long last_update_id;
		public int status;
		public ArrayList<FriendEntity> friends = new ArrayList<FriendEntity>();
		// public ArrayList<Long> delete_friends = new ArrayList<Long>();
	}

	public getAllFriendResult getOutput() {
		return (getAllFriendResult) GetResultObject();
	}

	public GetFriends(long last_update_id) {
		String url = APIConfiguration.getBaseUrl() + "Friend/Friend/getFriendList?"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		getAllFriendResult result = new getAllFriendResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		if (result.status == HttpInvokeResultCode.RESULT_OK) {

			JSONArray array = null;
			try {
				array = json.optJSONArray("data");
			} catch (Exception e) {
			}
			if (array == null) {
				return result;
			}

			result.friends = FriendSerializer.DeserializeFriends(array);

			// if (dataJson != null) {
			// result.last_update_id = dataJson.optLong("last_update_id");
			// if (dataJson.has("list")) {
			// JSONArray array = dataJson.optJSONArray("list");
			// if (array != null) {
			// result.friends = FriendSerializer
			// .DeserializeFriends(array);
			// }
			// }
			//
			// if (dataJson.has("delete_list")) {
			// JSONArray deleteList = dataJson.optJSONArray("delete_list");
			// if (deleteList != null && deleteList.length() > 0) {
			// int len = deleteList.length();
			// for (int i = 0; i < len; i++) {
			// result.delete_friends.add(deleteList.optLong(i, 0));
			// }
			// }
			// }
			//
			// }
		}
		return result;
	}
}
